package com.dongdongshop.front.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.cart.service.OrderService;
import com.dongdongshop.front.mongdb.Article;
import com.dongdongshop.front.mongdb.Comments;
import com.dongdongshop.front.mongdb.TbOrderVo;
import com.dongdongshop.front.pojo.TbUser;
import com.dongdongshop.front.service.MongdbService;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("mongdb")
public class MongdbController {

    Logger logger = LoggerFactory.getLogger(MongdbController.class);

    @Reference
    private OrderService orderService;
    @Reference
    private MongdbService mongdbService;

    //代评价
    @RequestMapping("toEvaluate")
    public String order(Model m, String statuc){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        List<TbOrderVo> orderVos =  orderService.selectStatuc(user.getUsername(),statuc);
        m.addAttribute("orderVos",orderVos);
        return "admin/home-order-evaluate";
    }

    //评论
    @RequestMapping("toAdd")
    public String toAdd(){
        return "admin/home-order-toAdd";
    }

    //提交评论
    @ResponseBody
    @RequestMapping("insertStatuc")
    public Result insertStatuc(Long orderId, String status, Comments comments){
        try {
           orderService.insertStatuc(orderId,status,comments);
            return Result.success(ResultEnum.SUCCESS,"评论提交成功");
        } catch (Exception e){
            logger.error("评论提交失败",e);
            return  Result.success(ResultEnum.SUCCESS,"评论提交失败");
        }
    }
    //查询评论
    @ResponseBody
    @CrossOrigin("*")//解决跨域
    @RequestMapping("selectMongoDB")
    public Result selectMongoDB (Long goodId){
        try {
            List<Comments> list = mongdbService.selectMongoDB(goodId);
            return Result.success(ResultEnum.SUCCESS,list);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.ERROR),e);
            return  Result.response(ResultEnum.ERROR);
        }
    }

}
